package com.jfl.web.controller.system;

import java.util.Arrays;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.jfl.annotation.Log;
import com.jfl.base.AbstractController;
import com.jfl.common.Constants;
import com.jfl.core.utils.ShiroUtils;
import com.jfl.enums.LogType;
import com.jfl.enums.Module;
import com.jfl.model.SysDictData;
import com.jfl.system.SysDictDataService;
import com.jfl.utils.ConvertRequestUtil;
import com.jfl.utils.ConvertUtil;

/**
 * 字典数据管理
 * 
 * @author 赵炎
 * @version [V1.00, 2018年12月28日]
 * @since V1.00
 */
@Controller
@RequestMapping("${jfl.module.system}/dict/data")
public class SysDictDataController extends AbstractController
{
    
    @Reference(version = "${jfl.version}")
    private SysDictDataService sysDictDataService;
    
    @RequiresPermissions("system:dict:view")
    @GetMapping()
    public String dictData()
    {
        return Constants.MODULE_SYS_DICT_TYPE_PREFIX + "data";
    }
    
    @PostMapping("/list")
    @RequiresPermissions("system:dict:list")
    @ResponseBody
    public ResponseEntity<ModelMap> list(SysDictData dictData)
    {
        PageInfo<SysDictData> pageInfo = this.sysDictDataService.selectList(ConvertRequestUtil.pageRequest(), dictData);
        return setSuccessModelMap(pageInfo);
    }
    
    /**
     * 新增字典类型
     */
    @GetMapping("/add/{dictType}")
    public String add(@PathVariable("dictType") String dictType, ModelMap mmap)
    {
        mmap.put("dictType", dictType);
        return Constants.MODULE_SYS_DICT_DATA_PREFIX + "add";
    }
    
    /**
     * 新增保存字典类型
     */
    @Log(module = Module.DICT, value = "添加字典数据", type = LogType.INSERT)
    @RequiresPermissions("system:dict:add")
    @PostMapping("/add")
    @ResponseBody
    public ResponseEntity<ModelMap> addSave(SysDictData dict)
    {
        dict.setCreateBy(ShiroUtils.getCurrentUserId());
        this.sysDictDataService.update(dict);
        return setSuccessModelMap();
    }
    
    /**
     * 修改字典类型
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("dict", this.sysDictDataService.queryById(id));
        return Constants.MODULE_SYS_DICT_DATA_PREFIX + "/edit";
    }
    
    /**
     * 修改保存字典类型
     */
    @Log(module = Module.DICT, value = "修改字典数据", type = LogType.UPDATE)
    @RequiresPermissions("system:dict:edit")
    @PostMapping("/edit")
    @ResponseBody
    public ResponseEntity<ModelMap> editSave(SysDictData dict)
    {
        dict.setUpdateBy(ShiroUtils.getCurrentUserId());
        this.sysDictDataService.update(dict);
        return setSuccessModelMap();
    }
    
    @Log(value = "删除字典数据", type = LogType.DELETE)
    @RequiresPermissions("system:dict:remove")
    @PostMapping("/remove")
    @ResponseBody
    public ResponseEntity<ModelMap> remove(String ids)
    {
        this.sysDictDataService.removeByIds(Arrays.asList(ConvertUtil.toLongArray(ids)));
        return setSuccessModelMap();
    }
}
